package interfazeak;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import db.BotikaKud;
import db.OharrakKud;

import javax.swing.JScrollBar;

public class Oharrak extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private Botikak guraso;
	private JButton gordebtn;
	private JButton atzerabtn;
	private JTextArea textArea;
	private JLabel oharralbl;
	private JTextField field;
	private JButton ezabatu;
	private JLabel mezua;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			final Boolean MODALA = true;
			Oharrak dialog = new Oharrak(new Botikak(), MODALA);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 * 
	 * @param b
	 * @param guraso
	 */
	public Oharrak(Botikak guraso, boolean b) {
		this.guraso = guraso;
		
		setBounds(100, 100, 748, 451);
		setTitle("OHARRAK");
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(5, 150, 600, 200);
		contentPanel.add(scrollPane);

		mezua = new JLabel ("Oharra:");
		mezua.setBounds(5, 30, 100, 20);
		contentPanel.add(mezua);
		
		field = new JTextField();
		field.setBounds(5, 50, 600 , 40);
		contentPanel.add(field);
		
		textArea = new JTextArea();
		textArea.setEditable(false);
		contentPanel.add(textArea);
		scrollPane.setViewportView(textArea);
	
	    	
		gordebtn = new JButton("GORDE");
		gordebtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				String idatzi = field.getText();
				if (idatzi.equals("")){
					JOptionPane.showMessageDialog(Oharrak.this,
							"Hutsik dago", "KONTUZ",
							JOptionPane.WARNING_MESSAGE);
				}
				else {
					int konfirmatu =JOptionPane.showConfirmDialog(Oharrak.this, "Gordetzea nahi duzu?");

				if (JOptionPane.OK_OPTION == konfirmatu){
				 Oharrak.this.oharrakgehitu(idatzi);
				Oharrak.this.ezabatuoharrak();
				Oharrak.this.bistaratu();
				field.setText(null);
				
				}
				else if(JOptionPane.NO_OPTION == konfirmatu) {
					
					Oharrak.this.dispose();
			}
				}
				
				
			}
		});
		gordebtn.setBounds(250,100,117,25);
		contentPanel.add(gordebtn);
	

		atzerabtn = new JButton("BOTIKAK");
		atzerabtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				Oharrak.this.dispose();
			}
		});
		atzerabtn.setBounds(350,380,117,25);
		contentPanel.add(atzerabtn);
		
		ezabatu = new JButton("DENA EZABATU");
		ezabatu.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				
				int konfirmatu =JOptionPane.showConfirmDialog(Oharrak.this, "Ezabatu nahi duzu?");

				if (JOptionPane.OK_OPTION == konfirmatu){
					Oharrak.this.ezabatuoharrak();
					OharrakKud.ezabatuOharrak();
				
				}
				
				
			}
		});
		ezabatu.setBounds(190,380,150,25);
		contentPanel.add(ezabatu);
		
		bistaratu();
	}

	protected void ezabatuoharrak() {
		
		textArea.setText("");
	}

	private void bistaratu() {
		ResultSet rs = OharrakKud.oharrakKargatu();
		int buelta= 0;
		try {
			while (rs.next()) {
				 String newline = "\n";
				 System.getProperty("line.separator");
				String oharra = rs.getString("oharra");
				textArea.append(oharra+"\n");
			
			}
		} catch (SQLException e) {

			e.printStackTrace();
		}
	}

	public void oharrakgehitu(String idatzi) {
		
		OharrakKud.oharrakgehitu(idatzi);
		
		
	}
	
}
